//
//  WJBaseViewController.swift
//  WJWEIBO
//
//  Created by 文杰 许                       on 2017/5/8.
//  Copyright © 2017年 文杰 许                      . All rights reserved.
//

import UIKit

// MARK: -- 基类控制的公共属性和方法
class WJBaseViewController: UIViewController
{    
    /// 自定义navigationBar
    lazy var navBar : UIView =
        {
            let navBar = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
            navBar.backgroundColor = UIColor(rgb: 0xF6F6F6)
            return navBar
    }()
    
    /// navigationBar title
    lazy var navItem : UIButton =
    {
        let item = UIButton(title: "", normalColor: UIColor.darkGray, highlightedColor: UIColor.darkGray)
        item.frame = CGRect(x: UIScreen.main.bounds.width / 2 - 22, y: 20, width: 50, height: 44)
        item.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        return item
    }()
    
    /// navigationBar 左边按钮
    lazy var leftBarItem : UIButton =
    {
        let leftItem = UIButton(title: "", normalColor: UIColor.gray, highlightedColor: UIColor.orange)
        leftItem.frame = CGRect(x: 8, y: 20, width: 50, height: 44)
        leftItem.isHidden = true
        return leftItem
    }()
    
    /// navigationBar 右边按钮
    lazy var rightBarItem : UIButton =
    {
        let rightItem = UIButton(title: "", normalColor: UIColor.gray, highlightedColor: UIColor.orange)
        rightItem.frame = CGRect(x: UIScreen.main.bounds.width - 60, y: 20, width: 50, height: 44)
        rightItem.isHidden = true
        return rightItem
    }()
    
    /// navigationBar 返回按钮
    lazy var backBarItem : UIButton =
    {
        let backBtn = UIButton(title: "", normalColor: UIColor.gray, highlightedColor: UIColor.orange)
        backBtn.frame = CGRect(x: 8, y: 20, width: 55, height: 44)
        backBtn.isHidden = true
        backBtn .setImage(#imageLiteral(resourceName: "btn_arrow_left"), for: .normal)
        backBtn .addTarget(self, action: #selector(backVC), for: .touchUpInside)
        return backBtn
    }()
    
    /// 列表
    var tableView : UITableView?
    
    /// 刷新控件
    var refreshControl : UIRefreshControl?
    
    /// 上拉加载标记
    var isPullup = false
    
    /// 是否登录标记
    var isLogin = true
    
    /// 访客视图信息
    var visitorInfoDict : [String : String]?
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        setupUI()
        
        loadData()
    }
    
    /// 重写title的didSet的方法
    override var title: String?
    {
        didSet
        {
            navItem.setTitle(title, for: .normal)
        }
    }
    
    /// 返回按钮点击事件
    @objc func backVC()
    {
        UIViewController.currentViewController().navigationController?.popViewController(animated: true)
    }
    
    /// 加载数据
    func loadData()
    {
        
    }
    
    /// 刷新数据
    func refreshData()
    {
        //base里要关闭刷新，避免继承base的子类，如果没有实现refreshData方法时，刷新菊花不会停止的问题
        refreshControl?.endRefreshing()
    }
}

/*
 extension注意点：
 1.extension中不能有属性
 2.extension中不能重写父类方法，重写父类方法是子类的职责，扩展是对类的扩展
 */
// MARK: --设置界面
extension WJBaseViewController
{
    
    /// 设置UI
    fileprivate func setupUI()
    {
        view.backgroundColor = UIColor.white
        
        setupNavigationBar()
        
        isLogin ? setupTableView() : setupVisitorView()
    }
    
    /// 列表
    func setupTableView()
    {
        tableView = UITableView(frame: CGRect(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height - 64), style: .plain)
        
        view.insertSubview(tableView!, belowSubview: navBar)
        
        tableView?.delegate = self
        tableView?.dataSource = self
        
        // 添加刷新控件
        refreshControl = UIRefreshControl()
        refreshControl?.attributedTitle = NSAttributedString(string: "加载中", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 12)])
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(refreshData), for: .valueChanged)
    }
    /// 访客
    fileprivate func setupVisitorView()
    {
        let visitorView = WJVisitorView(frame: view.bounds)
        visitorView.visitorInfo = visitorInfoDict;
        visitorView.loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.registerBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        view.insertSubview(visitorView, belowSubview: navBar)
        
        leftBarItem.isHidden = false
        rightBarItem.isHidden = false
        // 设置访客模式下，navBar上显示的注册、登录按钮
        leftBarItem.setTitle("注册", for: .normal)
        leftBarItem.setTitleColor(UIColor.orange, for: .normal)
        leftBarItem.setTitleColor(UIColor.lightGray, for: .highlighted)
        rightBarItem.setTitle("登录", for: .normal)
        rightBarItem.setTitleColor(UIColor.orange, for: .normal)
        rightBarItem.setTitleColor(UIColor.lightGray, for: .highlighted)
        
        leftBarItem.addTarget(self, action: #selector(register), for: .touchUpInside)
        rightBarItem.addTarget(self, action: #selector(login), for: .touchUpInside)
    }
    
    /// 设置导航条
    fileprivate func setupNavigationBar()
    {
        // 添加items到navBar
        navBar.addSubview(navItem)
        navBar.addSubview(leftBarItem)
        navBar.addSubview(rightBarItem)
        navBar.addSubview(backBarItem)
        // 添加导航条
        view.addSubview(navBar)
    }
}

// MARK: --UITableViewDelegate, UITableViewDataSource
extension WJBaseViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 0;
    }
    // 基类只是负责准备方法，子类负责具体实现
    // 子类重写不需要调用super方法
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        return UITableViewCell()
    }
    
    // cell将要出现的时候
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
    {
        let section = tableView.numberOfSections - 1
        let row = tableView.numberOfRows(inSection: section) - 1
        
        if section < 0 || indexPath.row < 0
        {
            return
        }
        
        // 如果是最后一行，同时没有做上拉加载
        if row == indexPath.row && !isPullup
        {
            isPullup = true
            
            refreshData()
        }
    }
}

// MARK：设置访客视图的监听方法
extension WJBaseViewController
{
    @objc fileprivate func login()
    {
        print("登录")
    }
    
    @objc fileprivate func register()
    {
        print("注册")
    }
}
